package jTableTest;
/**
 * GUI Demo
 *
 * (c) 2008 Nick Penaranda
*/

// Import all the classes used in this demo
import java.awt.Graphics;
import java.util.Vector;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

// Point class, stand-alone
class Point {
    // Internal coordinates
    protected int x,y;
    
    // Constructor, empty
    public Point() {
        x=y=0;
    }
    
    // Constructor, coordinate initializers
    public Point(int ix,int iy) {
        x=ix;
        y=iy;
    }
    
    // coordinate mutators
    public void setX(int ix) {
        x=ix;
    }
    
    public void setY(int iy) {
        y=iy;
    }
    
    // coordinate accessors
    public int getX() {
        return(x);
    }
    
    public int getY() {
        return(y);
    }
}

// Circle class, extends Point class with radius
class Circle extends Point {
  private int radius;
  
  // Constructor, blank
  public Circle() {
    radius = 0;
  }
  
  // Constructor, coordinate and radius initializers
  public Circle(int ix,int iy,int ir) {
    x=ix;
    y=iy;
    radius=ir;
  }
  
  // radius accessor
  public int getRadius() {
    return(radius);
  }
  
  // radius mutator
  public void setRadius(int ir) {
    radius=ir;
  }
}

// PointDemo class, extends JFrame, called when Point demo is selected
// from intial JOptionPane
class PointDemo extends javax.swing.JFrame {
  // Component declarations
  private javax.swing.JMenuBar jMenuBar;
  private javax.swing.JMenu jMenuExit;
  private javax.swing.JPanel jPanelMain;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextArea1;
  private javax.swing.JTextField jTextFieldStatusBar;

  // Private fields
  private Vector<Point> points;
  private int hIndex; // Highlight index
  
  // Constructor, blank
  public PointDemo() {
    points = new Vector<Point>();
    hIndex = 0;
    initComponents();
  }
  
  // Initializes window and components. Generated by NetBeans
  private void initComponents() {
    jPanelMain = new javax.swing.JPanel();
    jTextFieldStatusBar = new javax.swing.JTextField();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jMenuBar = new javax.swing.JMenuBar();
    jMenuExit = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Point Demo");
    setResizable(false);
    jPanelMain.setBackground(new java.awt.Color(255, 255, 255));
    jPanelMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    jPanelMain.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        jPanelMainMouseClicked(evt);
      }
    });
    jPanelMain.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
      public void mouseMoved(java.awt.event.MouseEvent evt) {
        jPanelMainMouseMoved(evt);
      }
    });

    javax.swing.GroupLayout jPanelMainLayout = new javax.swing.GroupLayout(jPanelMain);
    jPanelMain.setLayout(jPanelMainLayout);
    jPanelMainLayout.setHorizontalGroup(
      jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 354, Short.MAX_VALUE)
    );
    jPanelMainLayout.setVerticalGroup(
      jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 255, Short.MAX_VALUE)
    );

    jTextFieldStatusBar.setEditable(false);
    jTextFieldStatusBar.setText("Welcome to PointDemo");

    jTextArea1.setColumns(20);
    jTextArea1.setEditable(false);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jMenuExit.setText("Exit");
    jMenuExit.addMenuListener(new javax.swing.event.MenuListener() {
      public void menuCanceled(javax.swing.event.MenuEvent evt) {
      }
      public void menuDeselected(javax.swing.event.MenuEvent evt) {
      }
      public void menuSelected(javax.swing.event.MenuEvent evt) {
        jMenuExit(evt);
      }
    });

    jMenuBar.add(jMenuExit);

    setJMenuBar(jMenuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
          .addComponent(jTextFieldStatusBar)
          .addComponent(jPanelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addComponent(jPanelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTextFieldStatusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    pack();
  }
    
  // redrawMain method, updates main panel graphics when points are
  // added/removed
  private void redrawMain() {
      int x,y,n=0;
      Iterator<Point> i = points.iterator();
      Graphics g = jPanelMain.getGraphics();
      Point p;
      
      jTextArea1.setText("");
      
      // clear graphics panel
      g.clearRect(0,0,jPanelMain.getWidth(),jPanelMain.getHeight());
      
      // iterate through all points, drawing each as a '+' and also
      // updates right-hand list with point information.
      while(i.hasNext()) {
        p = i.next();
        x=p.getX();
        y=p.getY();
        g.drawLine(x-5,y,x+5,y);
        g.drawLine(x,y-5,x,y+5);
        jTextArea1.setText(jTextArea1.getText() + "Point " + n + ": " + x + "," + y + "\n");
        n++;
      }
    }
    
  // jPanelMainMouseClicked -- called when user clicks inside main panel,
  // adds points on left click, removes on right click
  private void jPanelMainMouseClicked(java.awt.event.MouseEvent evt) {
      int x,y;
      x = evt.getX();
      y = evt.getY();
      
      updateHighlightIndex(evt);
      
      // Switch left or right mouse buttons
      switch(evt.getButton()) {
        case java.awt.event.MouseEvent.BUTTON1: // left MB
        
          // Check to see if we are already on an existing point
          // if so, deny addition of duplicate point.
          if(hIndex>=0) {
            System.out.println("Duplicate point exists at " + x + "," + y + " (Point " + hIndex + ")");
          } 
          
          // otherwise, create new point
          else {
            System.out.println("Creating point at " + x + "," + y);
            points.addElement(new Point(x,y));
          }
          break;
          
        case java.awt.event.MouseEvent.BUTTON3: // right MB
        
          // Are we on an existing point?
          // if so, delete point
          if(hIndex>=0) {
            System.out.println("Deleting point at " + x + "," + y);
            points.removeElementAt(hIndex);
          } 
          
          // otherwise, notify user of no point to delete
          else {
            System.out.println("No point to delete!");
          }
          break;
      }
      
      // always redraw main after each mouse click, despite no action
      redrawMain();
    }
    
    // Called on each mouse click and/or mouse movement.
    // Determines if mouse coordinates match a point's coordinates,
    // If found, sets hIndex (Highlight Index) to that of the point's
    private void updateHighlightIndex(java.awt.event.MouseEvent evt) {
      int x,y,n=0;

      // Detect if we are on a point
      Iterator<Point> i = points.iterator();
      Point p;
      
      x=evt.getX();
      y=evt.getY();
      
      hIndex = -1;
      
      // iterate through all points until coordinates match mouse coords
      while(i.hasNext()) {
        p = i.next();
        if((p.getX()==x) && (p.getY()==y)) {
          hIndex=n;
          break;
        }
        n++;
      }
    }

    // jPanelMainMouseMoved -- called whenever mouse moves within
    // main panel
    private void jPanelMainMouseMoved(java.awt.event.MouseEvent evt) {
      updateHighlightIndex(evt);
      
      // Check to see if we are on a point. If so, display point info
      // on status bar.
      if(hIndex>=0)
        jTextFieldStatusBar.setText("(" + evt.getX() + "," + evt.getY() + ")" + " Point " + hIndex);
        
      // Otherwise, display mouse coordinates only.
      else
        jTextFieldStatusBar.setText("(" + evt.getX() + "," + evt.getY() + ")");
      
    }
    
    // jMenuExit -- called when "Exit" menu item is clicked
    private void jMenuExit(javax.swing.event.MenuEvent evt) {
      this.dispose();
    }
}

class CircleDemo extends javax.swing.JFrame {
  // Component declarations
  private javax.swing.JMenuBar jMenuBar;
  private javax.swing.JMenu jMenuExit;
  private javax.swing.JPanel jPanelMain;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextArea1;
  private javax.swing.JTextField jTextFieldStatusBar;

  // Private fields
  private Vector<Circle> circles;
  private boolean waitRadius; // Drawing radius??
  private int hIndex; // Highlight index
  private int cX,cY; // Coord buffers while waiting for radius

  // Constructor, blank
  public CircleDemo() {
    circles = new Vector<Circle>();
    hIndex = 0;
    waitRadius = false;
    cX=cY=0;
    initComponents();
  }
  
  // Initializes window and components. Generated by NetBeans
  private void initComponents() {
    jPanelMain = new javax.swing.JPanel();
    jTextFieldStatusBar = new javax.swing.JTextField();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jMenuBar = new javax.swing.JMenuBar();
    jMenuExit = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Circle Demo");
    setResizable(false);
    jPanelMain.setBackground(new java.awt.Color(255, 255, 255));
    jPanelMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    jPanelMain.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        jPanelMainMouseClicked(evt);
      }
    });
    jPanelMain.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
      public void mouseMoved(java.awt.event.MouseEvent evt) {
        jPanelMainMouseMoved(evt);
      }
    });

    javax.swing.GroupLayout jPanelMainLayout = new javax.swing.GroupLayout(jPanelMain);
    jPanelMain.setLayout(jPanelMainLayout);
    jPanelMainLayout.setHorizontalGroup(
      jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 354, Short.MAX_VALUE)
    );
    jPanelMainLayout.setVerticalGroup(
      jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 255, Short.MAX_VALUE)
    );

    jTextFieldStatusBar.setEditable(false);
    jTextFieldStatusBar.setText("Welcome to CircleDemo");

    jTextArea1.setColumns(20);
    jTextArea1.setEditable(false);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jMenuExit.setText("Exit");
    jMenuExit.addMenuListener(new javax.swing.event.MenuListener() {
      public void menuCanceled(javax.swing.event.MenuEvent evt) {
      }
      public void menuDeselected(javax.swing.event.MenuEvent evt) {
      }
      public void menuSelected(javax.swing.event.MenuEvent evt) {
        jMenuExit(evt);
      }
    });

    jMenuBar.add(jMenuExit);

    setJMenuBar(jMenuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
          .addComponent(jTextFieldStatusBar)
          .addComponent(jPanelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
            .addComponent(jPanelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTextFieldStatusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    pack();
  }
    
  // redrawMain method, updates main panel graphics when circles are
  // added/removed, also while waitingRadius is true.
  private void redrawMain() {
      int x,y,r,n=0;
      Iterator<Circle> i = circles.iterator();
      Graphics g = jPanelMain.getGraphics();
      Circle c;
      
      jTextArea1.setText("");
      
      // clear graphics pane
      g.clearRect(0,0,jPanelMain.getWidth(),jPanelMain.getHeight());
      
      // iterate through all existing circles and draws them with '+'
      // in center and outlying circle. also updates right hand list of
      // circles
      while(i.hasNext()) {
        c = i.next();
        x=c.getX();
        y=c.getY();
        r=c.getRadius();
        g.drawLine(x-5,y,x+5,y);
        g.drawLine(x,y-5,x,y+5);
        g.drawOval(x-r,y-r,r*2,r*2);
        jTextArea1.setText(jTextArea1.getText() + "Circle " + n + ": " + x + "," + y + " Radius " +
                r + "\n");
        n++;
      }
    }
    
    // jPanelMainMouseClicked -- called when user clicks inside main panel
    private void jPanelMainMouseClicked(java.awt.event.MouseEvent evt) {
      int x,y,r;
      x = evt.getX();
      y = evt.getY();
      
      updateHighlightIndex(evt);
      
      switch(evt.getButton()) {
        case java.awt.event.MouseEvent.BUTTON1:
          if(hIndex>=0) {
            System.out.println("Circle with same center exists at " + x + "," + y + " (Circle " + hIndex + ")");
          } else {
            if(waitRadius) {
              r=(int)Math.sqrt(Math.abs((x-cX)*(x-cX))+Math.abs((y-cY)*(y-cY)));
              System.out.println("Creating circle at " + cX + "," + cY);
              circles.addElement(new Circle(cX,cY,r));
              waitRadius = false;
            }
            else {
              waitRadius = true;
              cX=x;
              cY=y;
            }
          }
          break;
        case java.awt.event.MouseEvent.BUTTON3:
          if(hIndex>=0) {
            System.out.println("Deleting circle at " + x + "," + y);
            circles.removeElementAt(hIndex);
          } else {
            System.out.println("No circle to delete!");
          }
          break;
      }
      
      redrawMain();
    }
    
    // updates the highlight index, checks to see if mouse if hovering
    // over an existing center of a circle
    private void updateHighlightIndex(java.awt.event.MouseEvent evt) {
      int x,y,n=0;

      // Detect if we are on a circle's center
      Iterator<Circle> i = circles.iterator();
      Circle p;
      
      x=evt.getX();
      y=evt.getY();
      
      hIndex = -1;
      while(i.hasNext()) {
        p = i.next();
        if((p.getX()==x) && (p.getY()==y)) {
          hIndex=n;
          break;
        }
        n++;
      }
      
    }

    // jPanelMainMouseMoved -- called whenever mouse moves within
    // main panel
    private void jPanelMainMouseMoved(java.awt.event.MouseEvent evt) {
      updateHighlightIndex(evt);
      if(waitRadius) {
        Graphics g = jPanelMain.getGraphics();
        int x,y,r;
        x=evt.getX();
        y=evt.getY();
        r=(int)Math.sqrt(Math.abs((x-cX)*(x-cX))+Math.abs((y-cY)*(y-cY)));
        redrawMain();
        g.drawOval(cX-r,cY-r,r*2,r*2);
        jTextFieldStatusBar.setText("r = " + r);
      }
      else if(hIndex>=0) {
        double a,c;
        int r;
        r=circles.elementAt(hIndex).getRadius();
        a = Math.PI*r*r;
        c = 2*Math.PI*r;
        jTextFieldStatusBar.setText(String.format("(%d,%d) Circle %d (r = %d, A = %.2f, C = %.2f)",
                                    evt.getX(),evt.getY(),hIndex,r,a,c));
      }
      else
        jTextFieldStatusBar.setText("(" + evt.getX() + "," + evt.getY() + ")");
      
    }
    
    // jMenuExit -- called when "Exit" menu item is clicked
    private void jMenuExit(javax.swing.event.MenuEvent evt) {
      this.dispose();
    }
}

